package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.domain.Resource;
import com.itheima.manager.UmsResourceManager;
import com.itheima.vo.PageBeanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class UmsResourceController {
    @Autowired
    UmsResourceManager umsResourceManager;

    //根据条件分页模糊查询资源
    @GetMapping("/resource/list")
    public R<PageBeanVo> findByConditionAndPage(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                                @RequestParam(value="pageSize",defaultValue = "10")Integer pageSize,
                                                @RequestParam(value="nameKeyword",required = false) String nameKeyword,
                                                @RequestParam(value="urlKeyword",required = false) String urlKeyword,
                                                @RequestParam(value="categoryId",required = false) String categoryId){
        return umsResourceManager.findByConditionAndPage(pageNum,pageSize,nameKeyword,urlKeyword,categoryId);
    }

    //查询所有后台资源
    @GetMapping("/resource/listAll")
    public R<List<Resource>> findAll(){
        return umsResourceManager.findAll();
    }

    //修改后台资源
    @PostMapping("/resource/update/{id}")
    public R<String> updateResource(@PathVariable Long id,@RequestBody Resource resource){
        return umsResourceManager.updateResource(resource);
    }

    //根据ID获取资源详情
    @GetMapping("/resource/{id}")
    public R<Resource> findById(@PathVariable Long id){
        return umsResourceManager.findById(id);
    }

    //添加后台资源
    @PostMapping("/resource/create")
    public R<String> saveResource(@RequestBody Resource resource){
        return umsResourceManager.saveResource(resource);
    }

    //根据id删除资源
    @PostMapping("/resource/delete/{id}")
    public R<String> deleteResource(@PathVariable Long id){
        return umsResourceManager.deleteResource(id);
    }
}
